/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistance;

import java.sql.ResultSet;
import java.sql.SQLException;
import shared.Memory;

/**
 *
 * @author xarckane
 */
public class DaoMemory {

    private static DatabaseConnector connector = null;
    private static DaoMemory instance=null;

    public static DaoMemory getInstance() throws SQLException {
        if (null == instance) { // Premier appel
            instance = new DaoMemory();
             connector = DatabaseConnector.getInstance();
        }
        return instance;
    }

     void clear() throws SQLException {

        ResultSet resultat = connector.execQuery("select id from histo where type='mem'");
            
        while (resultat.next()) {

            connector.execUpdate("delete from  mem where histo_id=" + resultat.getInt("id"));
            connector.execUpdate("delete from histo where id="+ resultat.getInt("id"));

        }


    
}
private DaoMemory() {
    
    }
     public void saveMemory(String serv,Memory mem) throws SQLException  {
        Integer lastinserted_id = 0;

        ResultSet resultat;
       
            resultat = connector.execUpdate("INSERT INTO histo (serv,type) VALUES (\"" + serv + "\",\"mem\")");
        
       


            if (resultat.next()) {
                lastinserted_id = resultat.getInt(1);
                 resultat.close();
            } else {
               throw new SQLException("insertion ou récuperation de l'id impossible");
            }
        
           
      
        System.out.println(lastinserted_id);
         try {
             
       
        resultat = connector.execUpdate("INSERT INTO mem (histo_id,free,used,total) VALUES (" + lastinserted_id + "," + mem.getFree() + "," + mem.getUsed() + "," + mem.getTotal() + ")");
        } catch (SQLException e) {
            resultat.close();
            connector.execUpdate("delete  from histo where id="+lastinserted_id);
           throw e;
        }


            if (resultat.next()) {
                lastinserted_id = resultat.getInt(1);
                resultat.close();
            } else {
                connector.execUpdate("DELETE FROM histo where id="+lastinserted_id);
                throw new SQLException("insertion ou récuperation de l'id impossible");
            }

            
      
        System.out.println(lastinserted_id);
//            ResultSet resultat2 = connector.execQuery("INSERT INTO cpu (us,  sy,  ni,  id,  wa,  hi,  si,  st) VALUES (" + us + "," + sy + ")");

    }
     public Memory getMemoryByRequestId(int id) throws SQLException{
       
        System.out.println("select * from mem where histo_id="+id);
       ResultSet resultat = DatabaseConnector.getInstance().execQuery("select * from mem where histo_id="+id);
      Memory mem=null;
      
       while (resultat.next()) {
             mem=new shared.Memory(resultat.getInt("free"), resultat.getInt("used"), resultat.getInt("total"));
            }
       
        return mem;
    }


}
